1352D - Alice Bob and Candies - CodeForces Solution


implementation *1300

Please click on ads to support us..

Python Code:

t = int(input())

def solve():
    n = int(input())
    seq = list(map(int, input().split()))
    Ascore = 0
    Bscore = 0
    Aprev = 0
    Bprev = 0
    countMoves = 0
    AliceTUrn = True
    while n > 0:
        cur = 0
        if AliceTUrn:
            while cur <= Bprev and seq != []:
                cur += seq.pop(0)
                n -= 1
            Aprev = cur
            Ascore += cur
        else:
            while cur <= Aprev and seq != []:
                cur += seq.pop(-1)
                n -= 1
            Bprev = cur
            Bscore += cur
        AliceTUrn = not AliceTUrn
        countMoves += 1

    print(countMoves, Ascore, Bscore)





def yes(t):
    for _ in range(t):
        solve()

yes(t)

C++ Code:

#include <bits/stdc++.h>
using namespace std;
 
int main() {
    int t;
    cin>>t;
    while(t--){
        int n ;
        cin>>n;
        vector<long long > v(n);
        for(int i=0;i<n;i++){
            cin>>v[i];
        }
        int start = 0;
        int last = n-1;
        int moves = 0;
        int pre = 0;
        long long alice = 0;
        long long bob = 0;
        int chance = 0;
        
        while(last >= start){
            if(chance==0){
                long long temp = 0;
                while(start <= last && temp <= pre){
                    temp+=v[start++];
                }
                pre = temp;
                alice+=temp;
                chance = 1;
                
            }
            else{
                long long temp2 = 0;
                while(start <= last && temp2 <= pre){
                    temp2+=v[last--];
                }
                pre = temp2;
                bob+=temp2;
                chance = 0;
            }
            
            moves++;
        }
        cout<<moves<<" "<<alice<<" "<<bob<<endl;
    }
}


Comments

Submit
0 Comments
More Questions

144A - Arrival of the General
1106A - Lunar New Year and Cross Counting
58A - Chat room
230A - Dragons
200B - Drinks
13A - Numbers
129A - Cookies
1367B - Even Array
136A - Presents
1450A - Avoid Trygub
327A - Flipping Game
411A - Password Check
1520C - Not Adjacent Matrix
1538B - Friends and Candies
580A - Kefa and First Steps
1038B - Non-Coprime Partition
43A - Football
50A - Domino piling
479A - Expression
1480A - Yet Another String Game
1216C - White Sheet
1648A - Weird Sum
427A - Police Recruits
535A - Tavas and Nafas
581A - Vasya the Hipster
1537B - Bad Boy
1406B - Maximum Product
507B - Amr and Pins
379A - New Year Candles
1154A - Restoring Three Numbers